/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     scrollText.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-11
 * @brief    This file provides UartTFT Application display function
 ********************************************************************************/
#include "scrollText.h"
#include "levetop.h"
#include "data.h"
#include "keyBoard.h"
#include "popupBox.h"

uint8_t Scroll_num = 0;
uint8_t Scroll_flag = 0;
Scroll_Info gScroll_Info[SCROLL_SIZE];

void scroll_text(void)
{
	uint8_t i;
	if (Scroll_num && Scroll_flag)
	{
		Scroll_flag = 0;
		for (i = 0; i < Scroll_num; i++)
			gScroll_Info[i].t_c++;
		for (i = 0; i < Scroll_num; i++)
		{
			// When a window timing time expires, scroll 1 pixel
			if ((gScroll_Info[i].t_c >= gScroll_Info[i].t_d) && gScroll_Info[i].flag)
			{
				text_scroll_Once(i);
				gScroll_Info[i].t_c = 0;
			}
		}
	}
}
/***********************************************************************************
* Function Name	: text_scroll
* Description	: Get text scroll info.
* Input         : - type: text scroll info sources
				  - buff: text scroll info
* Output        : None
* Return        : None
************************************************************************************/
void text_scroll(uint8_t type, uint8_t *buff)
{
	uint16_t xs, ys, xe, ye; // Display box
	uint8_t Zk_id, w, h;	 // Font ID, font width and height
	uint8_t encode;			 // Font encoding type
	uint8_t Alignment;
	uint32_t fcolor, bcolor; // The foreground color of the font,The background color of the font
	uint16_t p_add, p_len, pointer;
	uint32_t zk_addr;
	uint8_t buff1[4] = {0};
	uint8_t scroll_en = 0;
	uint8_t t_d = 0;
	uint16_t gap = 0;
	uint8_t transparent = 0;

	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8)) * 2;
		p_len = buff[15] + (buff[16] << 8);
		xs = buff[7] + (buff[8] << 8);
		ys = buff[9] + (buff[10] << 8);
		xe = buff[11] + (buff[12] << 8);
		ye = buff[13] + (buff[14] << 8);
		Zk_id = buff[17];
		w = buff[18];
		h = buff[19];
		encode = buff[20];
		Alignment = buff[21];
		scroll_en = buff[22];
		fcolor = buff[23] + (buff[24] << 8) + (buff[25] << 16);
		t_d = buff[26];
		bcolor = buff[27] + (buff[28] << 8) + (buff[29] << 16);
		gap = buff[30] + (buff[31] << 8);
		transparent = buff[32];
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8)) * 2;
		p_len = var[pointer + 11] + (var[pointer + 12] << 8);
		xs = var[pointer + 3] + (var[pointer + 4] << 8);
		ys = var[pointer + 5] + (var[pointer + 6] << 8);
		xe = var[pointer + 7] + (var[pointer + 8] << 8);
		ye = var[pointer + 9] + (var[pointer + 10] << 8);
		Zk_id = var[pointer + 13];
		w = var[pointer + 14];
		h = var[pointer + 15];
		encode = var[pointer + 16];
		Alignment = var[pointer + 17];
		scroll_en = var[pointer + 18];
		fcolor = var[pointer + 19] + (var[pointer + 20] << 8) + (var[pointer + 21] << 16);
		t_d = var[pointer + 22];
		bcolor = var[pointer + 23] + (var[pointer + 24] << 8) + (var[pointer + 25] << 16);
		gap = var[pointer + 26] + (var[pointer + 27] << 8);
		transparent = var[pointer + 28];
	}

	if (Pop_up_flag == 1) // Popup offset
	{
		xs += Pop_up_x;
		ys += Pop_up_y;
		xe += Pop_up_x;
		ye += Pop_up_y;
	}

	LT_ReadFlash_UI(buff1, addr_index[2] + Zk_id * 8, 4);
	zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);

	gScroll_Info[Scroll_num].x = xs;
	gScroll_Info[Scroll_num].y = ys;
	gScroll_Info[Scroll_num].w = xe - xs + 1;
	gScroll_Info[Scroll_num].h = ye - ys + 1;
	gScroll_Info[Scroll_num].t_d = t_d;
	// gScroll_Info[Scroll_num].cnt = gScroll_Info[Scroll_num].w;
	gScroll_Info[Scroll_num].cnt = 0;
	gScroll_Info[Scroll_num].t_c = 0;
	gScroll_Info[Scroll_num].gap = gap;
	gScroll_Info[Scroll_num].transparent = transparent;
	gScroll_Info[Scroll_num].scroll_en = scroll_en;
	gScroll_Info[Scroll_num].bcolor = bcolor;
	gScroll_Info[Scroll_num].encode = encode;
	gScroll_Info[Scroll_num].zk_addr = zk_addr;
	gScroll_Info[Scroll_num].fcolor = fcolor;
	gScroll_Info[Scroll_num].Alignment = Alignment;
	gScroll_Info[Scroll_num].xe = xe;
	gScroll_Info[Scroll_num].ye = ye;
	gScroll_Info[Scroll_num].len_max = p_len * 2;
	gScroll_Info[Scroll_num].c = &var[p_add];
	gScroll_Info[Scroll_num].font_w = w;
	gScroll_Info[Scroll_num].font_h = h;
	gScroll_Info[Scroll_num].flag = 2;


	LT_Print_zk_Font_Scroll(encode, zk_addr,
							LAY_BUFF1, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, 0, PICINFO[0],
							xs, ys, xe, ye,
							0, 0, gScroll_Info[Scroll_num].c, p_len * 2,
							Scroll_num, 0);


	if(pic_update == 0)
	{
		if (Screen_DIR == 0 || Screen_DIR == 2)
		{
			LT268_buffer_to_TFT(gScroll_Info[Scroll_num].cnt, 0, gScroll_Info[Scroll_num].buf_w, m_PngData,
								gScroll_Info[Scroll_num].x, gScroll_Info[Scroll_num].y, gScroll_Info[Scroll_num].w, gScroll_Info[Scroll_num].h);
		}
		else if (Screen_DIR == 1 || Screen_DIR == 3)
		{
			LT268_buffer_to_TFT(0, gScroll_Info[Scroll_num].cnt, gScroll_Info[Scroll_num].w, m_PngData,
								gScroll_Info[Scroll_num].x, gScroll_Info[Scroll_num].y, gScroll_Info[Scroll_num].w, gScroll_Info[Scroll_num].h);
		}
	}

}

uint8_t text_scroll_Once(uint8_t num)
{
	uint16_t w_real = 0;
	// In the keyboard input interface
	if (DioKb_flag == 1)
	{
		if (!(gScroll_Info[num].x > gDio_Info[Dio_num].Xe || (gScroll_Info[num].x + gScroll_Info[num].w) < gDio_Info[Dio_num].Xs ||
			  gScroll_Info[num].y > gDio_Info[Dio_num].Ye || (gScroll_Info[num].y + gScroll_Info[num].h) < gDio_Info[Dio_num].Ys))
		{
			return 0;
		}
	}
	else if (AioKb_flag == 1)
	{
		if (!(gScroll_Info[num].x > gAio_Info[Aio_num].Xe || (gScroll_Info[num].x + gScroll_Info[num].w) < gAio_Info[Aio_num].Xs ||
			  gScroll_Info[num].y > gAio_Info[Aio_num].Ye || (gScroll_Info[num].y + gScroll_Info[num].h) < gAio_Info[Aio_num].Ys))
		{
			return 0;
		}
	}
	else if (GBKioKb_flag == 1)
	{
		if (!(gScroll_Info[num].x > gGBKio_Info[GBKio_num].Xe || (gScroll_Info[num].x + gScroll_Info[num].w) < gGBKio_Info[GBKio_num].Xs ||
			  gScroll_Info[num].y > gGBKio_Info[GBKio_num].Ye || (gScroll_Info[num].y + gScroll_Info[num].h) < gGBKio_Info[GBKio_num].Ys))
		{
			return 0;
		}
	}
	if (Screen_DIR == 0 || Screen_DIR == 2)
		w_real =  gScroll_Info[num].w;
	else if (Screen_DIR == 1 || Screen_DIR == 3)
		w_real =  gScroll_Info[num].h;

	if (gScroll_Info[num].cnt + w_real <= gScroll_Info[num].buf_w)
	{
		//LTPrintf("buf_w1 =%d \r\n",gScroll_Info[num].buf_w);
		LT_Print_zk_Font_Scroll(gScroll_Info[num].encode, gScroll_Info[num].zk_addr,
								LAY_BUFF1, LCD_XSIZE_TFT, gScroll_Info[num].font_w, gScroll_Info[num].font_h, gScroll_Info[num].Alignment,
								gScroll_Info[num].fcolor, gScroll_Info[num].bcolor, gScroll_Info[num].transparent, 0, PICINFO[0],
								gScroll_Info[num].x, gScroll_Info[num].y, gScroll_Info[num].xe, gScroll_Info[num].ye,
								0, 0, gScroll_Info[num].c, gScroll_Info[num].len_max,
								num, gScroll_Info[num].cnt);
		if (Screen_DIR == 0 || Screen_DIR == 2)
		{
			LT268_buffer_to_TFT(gScroll_Info[num].cnt, 0, gScroll_Info[num].buf_w, m_PngData,
								gScroll_Info[num].x, gScroll_Info[num].y, gScroll_Info[num].w, gScroll_Info[num].h);
		}
		else if (Screen_DIR == 1 || Screen_DIR == 3)
		{
			LT268_buffer_to_TFT(0 ,gScroll_Info[num].cnt, gScroll_Info[num].w, m_PngData,
								gScroll_Info[num].x, gScroll_Info[num].y, gScroll_Info[num].w, gScroll_Info[num].h);

		}
	}
	else
	{

		LT_Print_zk_Font_Scroll(gScroll_Info[num].encode, gScroll_Info[num].zk_addr,
								LAY_BUFF1, LCD_XSIZE_TFT, gScroll_Info[num].font_w, gScroll_Info[num].font_h, gScroll_Info[num].Alignment,
								gScroll_Info[num].fcolor, gScroll_Info[num].bcolor, gScroll_Info[num].transparent, 0, PICINFO[0],
								gScroll_Info[num].x, gScroll_Info[num].y, gScroll_Info[num].xe, gScroll_Info[num].ye,
								0, 0, gScroll_Info[num].c, gScroll_Info[num].len_max,
								num, gScroll_Info[num].cnt);

		if (gScroll_Info[num].cnt + w_real <= gScroll_Info[num].buf_w)
			return 0;
		if (Screen_DIR == 0 || Screen_DIR == 2)
		{
			LT268_buffer_to_TFT(gScroll_Info[num].cnt, 0, gScroll_Info[num].buf_w, m_PngData,
								gScroll_Info[num].x, gScroll_Info[num].y, gScroll_Info[num].buf_w - gScroll_Info[num].cnt , gScroll_Info[num].h);

			LT268_buffer_to_TFT(0, 0, gScroll_Info[num].buf_w, m_PngData,
								gScroll_Info[num].x + gScroll_Info[num].buf_w - gScroll_Info[num].cnt , gScroll_Info[num].y,
								gScroll_Info[num].w - (gScroll_Info[num].buf_w - gScroll_Info[num].cnt), gScroll_Info[num].h);
		}
		else if (Screen_DIR == 1 || Screen_DIR == 3)
		{

			LT268_buffer_to_TFT(0, 0, gScroll_Info[num].w, m_PngData,
								gScroll_Info[num].x , gScroll_Info[num].y + gScroll_Info[num].buf_w - gScroll_Info[num].cnt,
								gScroll_Info[num].w, gScroll_Info[num].h - (gScroll_Info[num].buf_w - gScroll_Info[num].cnt));

			LT268_buffer_to_TFT(0, gScroll_Info[num].cnt, gScroll_Info[num].w, m_PngData,
								gScroll_Info[num].x, gScroll_Info[num].y, gScroll_Info[num].w, gScroll_Info[num].buf_w - gScroll_Info[num].cnt);

		}
	}


	if (Screen_DIR == 0 || Screen_DIR == 1)
	{
		gScroll_Info[num].cnt += 1;
		if (gScroll_Info[num].cnt >= gScroll_Info[num].buf_w)
			gScroll_Info[num].cnt = 0;
	}
	else if (Screen_DIR == 2 || Screen_DIR == 3)
	{
		if (gScroll_Info[num].cnt == 0)
			gScroll_Info[num].cnt = gScroll_Info[num].buf_w;
		gScroll_Info[num].cnt--;
	}
}








